﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MRBrowserLibrary
{
    public class EventElement
    {
        private WebBrowser webBrowser;
        private string id;
        private HtmlElementEventHandler handler;

        public void click(Object o, HtmlElementEventArgs target)
        {
            MessageBox.Show("you click me");
        }

        public EventElement(WebBrowser webBrowser, string id)
        {
            this.webBrowser = webBrowser;
            if (webBrowser.Document != null)
            {
                var elementById = webBrowser.Document.GetElementById(id);
                if (elementById != null)
                {
                    elementById.Click += click;
                }
            }
        }

        public EventElement(WebBrowser webBrowser, string id, HtmlElementEventHandler handler, bool bindNow)
        {
            this.id = id;
            this.webBrowser = webBrowser;
            this.handler = handler;
            if (bindNow == true)
            {
                Bind();
            }
        }

        public void Bind()
        {
            if (webBrowser.Document != null)
            {
                var elementById = webBrowser.Document.GetElementById(id);
                if (elementById != null && handler != null)
                {
                    elementById.Click += handler;
                }
            }
        }
    }
}
